We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

$this->request->getPost() and $form->request->getPost()

Is there any difference (internal or practical) in using $this->request->getPost() and $form->request->getPost() in regards to filtering and sanitizing data coming from a form?

Example code:

            $form = new LoginForm();

            if ($this->request->isPost()) {
                if ($form->isValid($this->request->getPost())) {
                    // And here comes the line I am asking the question about
                    $this->getDI()->get('auth')->formLogin($form->request->getPost());
                    $this->flashSession->success('Login successful!');
                    // bla bla
                }
            }
// OR this
            $form = new LoginForm();

            if ($this->request->isPost()) {
                if ($form->isValid($this->request->getPost())) {
                    /// And here comes the line I am asking the question about
                    $this->getDI()->get('auth')->formLogin($this->request->getPost());
                    $this->flashSession->success('Login successful!');
                    // bla bla
                }
            }


11.2k
Accepted
answer
edited Nov '14

Both are being returned from DI.

It is a feature that \Phalcon\Forms\Form get injected with the DI and you can access their components.

So, basically $this->request === $form->request

Edit: Note, it is the same with every component that is injectable with the DI.

And it is better to use $this as you might at any time want to remove the $form and it would break your code